package com.ruoyi.project.erp.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.erp.domain.Project;
import com.ruoyi.project.erp.domain.Worklog;
import com.ruoyi.project.erp.dto.MyWorklogQuery;
import com.ruoyi.project.erp.dto.SalesStatQuery;
import com.ruoyi.project.erp.dto.WorklogStatQuery;
import com.ruoyi.project.erp.service.IAdminOaService;
import com.ruoyi.project.erp.service.IProjectService;
import com.ruoyi.project.erp.service.ITaskService;
import com.ruoyi.project.erp.service.IWorklogService;
import com.ruoyi.project.erp.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/oa")
public class AdminOaController extends BaseController {
    @Autowired
    IProjectService projectService;
    @Autowired
    IWorklogService worklogService;
    @Autowired
    ITaskService taskService;

    @Autowired
    IAdminOaService adminOaService;
    @GetMapping("/getUserProject")
    public TableDataInfo getUserProject(@RequestParam("userId")String userId){
        startPage();
        List<Project> projectList = projectService.getUserProjectInfo(userId);
    return getDataTable(projectList);
    }


    @GetMapping("/getUserTask")
    public AjaxResult getUserTask(@RequestParam("projectId")String projectId){
        List<UserTaskVO> projectList = taskService.getUserTaskInfo(projectId);
        return AjaxResult.success(projectList);
    }

    @GetMapping("/getWorkLogInfo")
    public AjaxResult getWorkLogInfo(@RequestParam("taskId")String taskId){
        List<UserWorklogInfoVO> worklogInfoVO = worklogService.getUserWorklogInfo(taskId);
        return AjaxResult.success(worklogInfoVO);
    }

    /*
    项目任务视角
     */
    /**
     * 获取项目工时概览
     */

    @GetMapping("/overview")
    public AjaxResult getOverview(WorklogStatQuery query) {
        ProjectStatOverviewVo overview = adminOaService.getProjectOverview(query);
        return AjaxResult.success(overview);
    }

    /**
     * 按成员统计工时列表
     */

    @GetMapping("/by-member")
    public TableDataInfo listStatsByMember(WorklogStatQuery query) {
        startPage(); // RuoYi 分页
        List<ProjectStatMemberVo> list = adminOaService.selectStatsByMemberList(query);
        return getDataTable(list);
    }

    /**
     * 按任务统计工时列表
     */

    @GetMapping("/by-task")
    public TableDataInfo listStatsByTask(WorklogStatQuery query) {
        startPage();
        List<ProjectStatTaskVo> list = adminOaService.selectStatsByTaskList(query);
        return getDataTable(list);
    }

    /**
     * 获取项目工时明细列表
     */

    @GetMapping("/project/details")
    public TableDataInfo listDetails(WorklogStatQuery query) {
        startPage();
        List<TaskDetialVO> list = adminOaService.selectWorklogDetailList(query);
        return getDataTable(list);
    }








    /*
    销售视角
     */
    /**
     * 获取销售业绩统计列表
     */

    @GetMapping("/sale/summary")
    public TableDataInfo getSummary(SalesStatQuery query) {
        // 注意：summary视图通常不需要分页，如果销售人数很多才需要
        // startPage();
        List<SalesStatSummaryVo> list = adminOaService.selectSalesSummaryStats(query);
        return getDataTable(list);
    }


    /*
    个人视角
     */
    /**
     * 获取我的工时汇总信息
     */

    @GetMapping("/user/summary")
    public AjaxResult getSummary(MyWorklogQuery query) {
        MyWorklogSummaryVo summary = adminOaService.getMyWorklogSummary(query);
        return AjaxResult.success(summary);
    }

    /**
     * 查询我的工时明细列表
     */

    @GetMapping("/user/list")
    public TableDataInfo list(MyWorklogQuery query) {
        startPage();
        List<TaskDetialVO> list = adminOaService.selectMyWorklogList(query);
        return getDataTable(list);
    }

}
